package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysWorks;
import com.ruoyi.system.mapper.SysWorksMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysVoteMapper;
import com.ruoyi.system.domain.SysVote;
import com.ruoyi.system.service.ISysVoteService;

/**
 * 投票Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-30
 */
@Service
public class SysVoteServiceImpl implements ISysVoteService 
{
    @Autowired
    private SysVoteMapper sysVoteMapper;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    private SysWorksMapper sysWorksMapper;

    /**
     * 查询投票
     * 
     * @param voteKey 投票主键
     * @return 投票
     */
    @Override
    public SysVote selectSysVoteByVoteKey(String voteKey)
    {
        return sysVoteMapper.selectSysVoteByVoteKey(voteKey);
    }

    /**
     * 查询投票列表
     * 
     * @param sysVote 投票
     * @return 投票
     */
    @Override
    public List<SysVote> selectSysVoteList(SysVote sysVote)
    {
        return sysVoteMapper.selectSysVoteList(sysVote);
    }

    /**
     * 新增投票
     * 
     * @param sysVote 投票
     * @return 结果
     */
    @Override
    public int insertSysVote(SysVote sysVote)
    {
        sysVote.setVoteKey(UUID.randomUUID().toString());
        LoginUser loginUser = (LoginUser) SecurityUtils.getAuthentication().getPrincipal();
        Long userId = loginUser.getUser().getUserId();
        sysVote.setCreateBy(userId.toString());
        sysVote.setCreateTime(DateUtils.getNowDate());
        return sysVoteMapper.insertSysVote(sysVote);
    }

    /**
     * 修改投票
     * 
     * @param sysVote 投票
     * @return 结果
     */
    @Override
    public int updateSysVote(SysVote sysVote)
    {
        LoginUser loginUser = (LoginUser) SecurityUtils.getAuthentication().getPrincipal();
        Long userId = loginUser.getUser().getUserId();
        sysVote.setUpdateBy(userId.toString());
        sysVote.setUpdateTime(DateUtils.getNowDate());
        return sysVoteMapper.updateSysVote(sysVote);
    }

    /**
     * 批量删除投票
     * 
     * @param voteKeys 需要删除的投票主键
     * @return 结果
     */
    @Override
    public int deleteSysVoteByVoteKeys(String[] voteKeys)
    {
        return sysVoteMapper.deleteSysVoteByVoteKeys(voteKeys);
    }

    /**
     * 删除投票信息
     * 
     * @param voteKey 投票主键
     * @return 结果
     */
    @Override
    public int deleteSysVoteByVoteKey(String voteKey)
    {
        return sysVoteMapper.deleteSysVoteByVoteKey(voteKey);
    }

    @Override
    public int countByWorksKey(String worksKey) {
        return sysVoteMapper.countByWorksKey(worksKey);
    }

    @Override
    public AjaxResult toupiao(SysVote sysVote) {
        SysVote sysVote1 = new SysVote();
        sysVote1.setVoteUserId(sysVote.getVoteUserId());
        // 获取当前0点时间Data
        sysVote1.setCreateTime(DateUtils.toDate(LocalDate.now()));
        List<SysVote> sysVotes = sysVoteMapper.selectSysVoteList(sysVote1);
        if (sysVotes != null && sysVotes.size() > 0) {
            if(sysVotes.size() >= 10){
               return AjaxResult.error("每个人每天最多投10票");
            }
        }
        SysUser sysUser = sysUserService.selectUserById(sysVote.getVoteUserId());
        sysVote.setVoteUserName(sysUser.getNickName());
        sysVote.setLoginIp(sysUser.getLoginIp());
        sysVote.setVoteKey(UUID.randomUUID().toString());
        sysVote.setCreateBy(sysVote.getVoteUserId().toString());
        sysVote.setCreateTime(DateUtils.getNowDate());
        sysVoteMapper.insertSysVote(sysVote);
        SysWorks sysWorks = new SysWorks();
        SysWorks sysWorks1 = sysWorksMapper.selectSysWorksByWorksKey(sysVote.getWorksKey());
        sysWorks.setWorksKey(sysVote.getWorksKey());
        int vote = Integer.parseInt(sysWorks1.getWorksVote())+1;
        sysWorks.setWorksVote(String.valueOf(vote));
        sysWorksMapper.updateSysWorks(sysWorks);
        return AjaxResult.success("投票成功");
    }
}
